package com.ctgu.controller;

import com.ctgu.entity.DataPage;
import com.ctgu.entity.PageBean;
import com.ctgu.entity.User;
import com.ctgu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {


    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    @RequestMapping ("login")
    public ModelAndView login(User user, HttpSession session ) {
        boolean flag=userService.login(user);
        ModelAndView modelAndView = new ModelAndView();
        if (flag) {
            session.setAttribute("user", user);
            modelAndView.setViewName("index1");
        } else {
            modelAndView.setViewName("/login.jsp");
        }
        return modelAndView;
    }

    @RequestMapping("allUser")
    public String list(Model model){
        List<User>  list = userService.getAllUserList();

        model.addAttribute("list",list);
        return "allUser";
    }


    //跳转到增加用户页面
    @RequestMapping("toAddUser")
    public String toAddPage(){
        return "addUser";
    }

    //添加用户请求
    @RequestMapping("addUser")
    public String addUser(User user){
        userService.addUser(user);
        return "redirect:allUser";
    }


    //跳转到修改界面
    @RequestMapping("/toUpdatePage")
    public String toUpdatePage(int id ,  Model model){
        User user = userService.getUser(id);
        model.addAttribute("getUser",user);
        return "updateUser";
    }

    //修改用户
    @RequestMapping("updateUser")
    public String updateUser(User user){
        userService.updateUser(user);
        return "redirect:allUser";
    }

    //删除用户
    @RequestMapping("deleteUser")
    public String deleteUser(int id){
        userService.deleteUserById(id);
        return "redirect:allUser";
    }
    //查询用户
    @RequestMapping("queryUser")
    public String queryUser(String username,Model model){
        List<User> list = userService.queryUserByName(username);
        if(list.size()==0){
            list = userService.getAllUserList();
            model.addAttribute("error","未查到");
        }
        model.addAttribute("list",list);
        return "allUser";
    }
    //分页
    @ResponseBody
    @RequestMapping("/listData")
    public DataPage listData(Integer page, Integer limit){
        List<User> list = userService.getUserListByLimit(page, limit);
        int count = userService.getUserCount();
        DataPage dataPage = new DataPage();
        dataPage.setData(list);
        dataPage.setCount(count);
        return dataPage;
    }
}
